Imports System.Data

Partial Class Admin_Add_Funds
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.SetPageAccess(Session("UserName"))
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 4, Secure.ControlTypes.EditType)
        sec = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If broker_cmb.Items.Count < 1 Then
            ViewState.Clear()

            Dim zClass As New LookUp.General_Lookup
            broker_cmb.Items.Clear()
            zClass.Loader(broker_cmb, "BROKER", "broker_desc", "broker_cd")
            broker_cmb.Items.Insert(0, "")

            currency_cmb.Items.Clear()
            zClass.Loader(currency_cmb, "CURRENCY", "Desc", "ID")
            currency_cmb.Items.Insert(0, "")

            zClass.LookUpCode = "FUNDS"
            DataGrid.CurrentPageIndex = 0
            zClass.DataGridFill(DataGrid, Results_lbl)

            Call PopulateBrokers()
        End If

        Dim Edit_Image As Image = CType(Me.Master.FindControl("L11"), Image)
        Edit_Image.ImageUrl = "~/images/redT.PNG"
        Dim Edit_Button As LinkButton = CType(Me.Master.FindControl("Funds_btn"), LinkButton)
        Edit_Button.ForeColor = Drawing.Color.Black
        Dim Edit_Panel As Panel = CType(Me.Master.FindControl("Lookup_Panel"), Panel)
        Edit_Panel.Visible = True
        SecurePage()
    End Sub

    Protected Sub broker_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles broker_cmb.SelectedIndexChanged
        If broker_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        provider_cmb.Items.Clear()
        zClass.Loader(provider_cmb, "PROVIDER", "provider_desc", "provider_cd", broker_cmb.SelectedValue)
        provider_cmb.Items.Insert(0, "")

    End Sub

    Protected Sub provider_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles provider_cmb.SelectedIndexChanged
        If provider_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        product_cmb.Items.Clear()
        zClass.Loader(product_cmb, "PRODUCT", "prdt_desc", "prdt_cd", provider_cmb.SelectedValue)
        product_cmb.Items.Insert(0, "")

    End Sub

    Protected Sub product_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles product_cmb.SelectedIndexChanged
        Call DoSearch()

    End Sub

    Private Sub DoSearch()
        If product_cmb.Items.Count = 0 Then Exit Sub
        If product_cmb.SelectedItem.Text = "" Or AddEdit_Panel.Visible = True Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "FUNDS"
        DataGrid.CurrentPageIndex = 0
        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    End Sub

    Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
        OnMode("Add") : ViewState("mode") = "Add"
        Code_txt.Text = "" : Name_txt.Text = ""
        currency_cmb.SelectedItem.Selected = False
        currency_cmb.SelectedIndex = 0
        Code_txt.Focus()
    End Sub

    Private Sub OnMode(ByVal mode As String)

        If mode = "Edit" Then
            broker_cmb.Enabled = False
            provider_cmb.Enabled = False
            product_cmb.Enabled = False
            Code_txt.Enabled = False
        End If
        DataGrid.Enabled = False
        Add_btn.Enabled = False
        AddEdit_Panel.Visible = True

    End Sub

    Private Sub NotOnMode()
        broker_cmb.Enabled = True
        provider_cmb.Enabled = True
        product_cmb.Enabled = True
        Code_txt.Enabled = True
        DataGrid.Enabled = True
        Add_btn.Enabled = True
        AddEdit_Panel.Visible = False

    End Sub

    'prdt_cd 4
    'fund_cd 0
    Protected Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.DeleteCommand
        Dim dao As New DAOAdmin.FUND_MSTR_DAO
        Dim vo As New DAOAdmin.FUND_MSTR_VO
        Dim zClass As New LookUp.General_Lookup

        If isDeleteOK(e.Item.Cells(0).Text, e.Item.Cells(4).Text) Then
            vo.FUND_CD = e.Item.Cells(0).Text
            vo.PRDT_CD = e.Item.Cells(4).Text

            dao.Delete(vo)
        End If

        zClass.LookUpCode = "FUNDS"
        DataGrid.CurrentPageIndex = 0
        If product_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If

        dao = Nothing
        vo = Nothing
        zClass = Nothing
    End Sub

    Private Function isDeleteOK(ByVal fund_cd As String, ByVal prdt_cd As String) As Boolean
        Dim blnReturn As Boolean = True

        Dim vo As New DAOMain.POLICY_FUND_DTL_VO
        Dim dao As New DAOMain.POLICY_FUND_DTL_DAO

        vo.FUND_CD = fund_cd
        vo.PRDT_CD = prdt_cd

        Dim dt As DataTable = dao.Search(vo)
        If dt.Rows.Count > 0 Then blnReturn = False

        dt = Nothing
        vo = Nothing
        dao = Nothing

        Return blnReturn
    End Function

#Region "MPZ Delete Code"
    'Protected Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.DeleteCommand
    '    Dim zClass As New LookUp.General_Lookup
    '    Dim dm As New DAOMain.CommonDAO
    '    zClass.LookUpCode = "FUNDS"

    '    If zClass.OKtoDELETE(Trim(e.Item.Cells(0).Text)) = False Then Exit Sub

    '    zClass.ExecQuery("DELETE FROM fund_mstr WHERE fund_cd=" & dm.Q(Trim(e.Item.Cells(0).Text)) & _
    '      " AND prdt_cd=" & dm.Q(Trim(e.Item.Cells(4).Text)))

    '    DataGrid.CurrentPageIndex = 0
    '    If product_cmb.SelectedValue <> "" Then
    '        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    '    Else
    '        zClass.DataGridFill(DataGrid, Results_lbl)
    '    End If

    'End Sub
#End Region

    Protected Sub DataGrid_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.EditCommand
        ViewState("mode") = "Edit"

        Dim dm As New DAOMain.CommonDAO

        Dim dt As DataTable = _
                    dm.ExecQuery("SELECT A.broker_cd, B.provider_cd, C.prdt_cd, D.fund_cd" & _
                    " FROM broker_mstr AS A INNER JOIN" & _
                        " provider_mstr AS B ON A.broker_cd = B.broker_cd INNER JOIN" & _
                        " prdt_mstr AS C ON B.provider_cd = C.provider_cd INNER JOIN" & _
                        " fund_mstr AS D ON C.prdt_cd = D.prdt_cd" & _
                    " Where D.fund_cd = " & dm.Q(e.Item.Cells(0).Text))

        If dt.Rows.Count > 0 Then
            broker_cmb.SelectedValue = dt.Rows(0)("broker_cd")

            Dim zClass As New LookUp.General_Lookup

            provider_cmb.Items.Clear()
            zClass.Loader(provider_cmb, "PROVIDER", "provider_desc", "provider_cd", broker_cmb.SelectedValue)
            provider_cmb.Items.Insert(0, "")

            provider_cmb.SelectedValue = dt.Rows(0)("provider_cd")

            product_cmb.Items.Clear()
            zClass.Loader(product_cmb, "PRODUCT", "prdt_desc", "prdt_cd", provider_cmb.SelectedValue)
            product_cmb.Items.Insert(0, "")

            product_cmb.SelectedValue = dt.Rows(0)("prdt_cd")
        End If

        Code_txt.Text = e.Item.Cells(0).Text
        Name_txt.Text = e.Item.Cells(1).Text
        currency_cmb.SelectedItem.Selected = False
        currency_cmb.Items.FindByValue(e.Item.Cells(5).Text).Selected = True
        ViewState("prdt_cd") = Trim(e.Item.Cells(4).Text)
        OnMode("Edit")
        Name_txt.Focus()
    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("prdt_cd")
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dao As New DAOAdmin.FUND_MSTR_DAO
        Dim vo As New DAOAdmin.FUND_MSTR_VO

        With vo
            .FUND_CD = Code_txt.Text
            .FUND_DESC = Name_txt.Text
            .CURRENCY_CD = currency_cmb.SelectedValue.ToString
        End With

        Select Case ViewState("mode")
            Case "Add"
                If isExist(Code_txt.Text, product_cmb.SelectedValue.ToString) = False Then
                    vo.PRDT_CD = product_cmb.SelectedValue.ToString
                    vo.FUND_CREA_BY = Session("UserName")
                    vo.FUND_CREA_DT = Now

                    dao.Create_Record(vo)
                Else
                    Dim pf As New PolyFunc.ASPFunc

                    pf.MessageAlert(Me, "Fund Already Exists!")
                    pf = Nothing
                End If
            Case "Edit"
                vo.PRDT_CD = ViewState("prdt_cd")
                vo.FUND_LAST_UPD_BY = Session("UserName")
                vo.FUND_LAST_UPD_DT = Now

                dao.Update(vo)
        End Select

        zClass.LookUpCode = "FUNDS"
        zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
        NotOnMode()
        ViewState.Remove("mode")
        ViewState.Remove("prdt_cd")

        dao = Nothing
        vo = Nothing
    End Sub

#Region "MPZ Save Code"
    'Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
    '    Dim zClass As New LookUp.General_Lookup
    '    Dim dm As New DAOMain.CommonDAO

    '    If ViewState("mode") = "Add" Then
    '        zClass.LookUpCode = "FUND"
    '        If zClass.DuplicateCode(Code_txt.Text) = False Then
    '            Dim pf As New PolyFunc.ASPFunc
    '            pf.MessageAlert(Me, "Fund Code already exists!")
    '            pf = Nothing
    '            Exit Sub
    '        End If
    '        zClass.ExecQuery("INSERT INTO fund_mstr(fund_cd, prdt_cd, fund_desc, currency_cd, " & _
    '            "fund_crea_by, fund_crea_dt) VALUES(" & _
    '            UCase(dm.Q(Trim(Code_txt.Text))) & ",'" & UCase(Trim(product_cmb.SelectedValue.ToString)) & "'," & _
    '            UCase(dm.Q(Trim(Name_txt.Text))) & ",'" & UCase(Trim(currency_cmb.SelectedValue.ToString)) & "'," & _
    '            "'" & UCase(Session("UserName")) & "','" & Now & "')")

    '    ElseIf ViewState("mode") = "Edit" Then
    '        zClass.ExecQuery("UPDATE fund_mstr SET " & _
    '            "fund_desc=" & UCase(dm.Q(Trim(Name_txt.Text))) & ", " & _
    '            "currency_cd='" & UCase(Trim(currency_cmb.SelectedValue.ToString)) & "', " & _
    '            "fund_last_upd_by='" & UCase(Session("UserName")) & "', " & _
    '            "fund_last_upd_dt='" & Now & "' " & _
    '            "WHERE fund_cd=" & UCase(dm.Q(Trim(Code_txt.Text))) & " AND prdt_cd='" & _
    '            ViewState("prdt_cd") & "'")

    '    End If

    '    zClass.LookUpCode = "FUNDS"
    '    zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
    '    NotOnMode()
    '    ViewState.Remove("mode")
    '    ViewState.Remove("prdt_cd")
    'End Sub
#End Region

    Private Function isExist(ByVal fund_cd As String, ByVal prdt_cd As String) As Boolean
        Dim blnReturn As Boolean = False
        Dim dao As New DAOAdmin.FUND_MSTR_DAO
        Dim vo As New DAOAdmin.FUND_MSTR_VO

        With vo
            .FUND_CD = fund_cd
            .PRDT_CD = prdt_cd
        End With

        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            blnReturn = True
        End If

        dt = Nothing
        dao = Nothing
        vo = Nothing

        Return blnReturn
    End Function

    Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
        DataGrid.CurrentPageIndex = e.NewPageIndex
        Dim zClass As New LookUp.General_Lookup
        zClass.LookUpCode = "FUNDS"

        If product_cmb.SelectedValue <> "" Then
            zClass.DataGridFill(DataGrid, Results_lbl, product_cmb.SelectedValue)
        Else
            zClass.DataGridFill(DataGrid, Results_lbl)
        End If

    End Sub

#Region "Copy Funds"
    Protected Sub btnCancelCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelCopy.Click
        ClearFields()
        pnlCopy.Visible = False
        divEntry.Visible = True
    End Sub

    Protected Sub lbtnCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnCopy.Click
        pnlCopy.Visible = True
        divEntry.Visible = False
    End Sub

    Private Sub PopulateBrokers()
        Dim lfunc As New LookUp.FillLookUp

        lfunc.FillDropDown(ddlBrokerSource, LookUp.FillLookUp.eLookUpType.broker)
        lfunc.FillDropDown(ddlBrokerDest, LookUp.FillLookUp.eLookUpType.broker)

        lfunc = Nothing
    End Sub

    Protected Sub ddlBrokerSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBrokerSource.SelectedIndexChanged
        Dim lfunc As New LookUp.FillLookUp

        lfunc.FillDropDown(ddlProviderSource, LookUp.FillLookUp.eLookUpType.provider, , ddlBrokerSource.SelectedValue)
        lfunc = Nothing
    End Sub

    Protected Sub ddlProviderSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProviderSource.SelectedIndexChanged
        Dim lfunc As New LookUp.FillLookUp

        lfunc.FillDropDown(ddlProductSource, LookUp.FillLookUp.eLookUpType.prdt, , ddlProviderSource.SelectedValue)
        lfunc = Nothing
    End Sub

    Protected Sub ddlProductSource_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProductSource.SelectedIndexChanged
        'Populate Grid
        Dim dao As New DAOAdmin.FUND_MSTR_DAO
        Dim vo As New DAOAdmin.FUND_MSTR_VO

        vo.PRDT_CD = ddlProductSource.SelectedValue
        Dim dt As DataTable = dao.Search(vo)

        ViewState.Add("dt", dt)
        dgSource.CurrentPageIndex = 0
        dgSource.DataSource = ViewState("dt")
        dgSource.DataBind()

        dao = Nothing
        vo = Nothing
    End Sub

    Protected Sub dgSource_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgSource.PageIndexChanged
        If e.NewPageIndex + 1 > dgSource.PageCount Then Exit Sub

        dgSource.CurrentPageIndex = e.NewPageIndex
        dgSource.DataSource = ViewState("dt")
        dgSource.DataBind()
    End Sub

    Protected Sub ddlBrokerDest_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlBrokerDest.SelectedIndexChanged
        Dim lfunc As New LookUp.FillLookUp

        lfunc.FillDropDown(ddlProviderDest, LookUp.FillLookUp.eLookUpType.provider, , ddlBrokerDest.SelectedValue)
        lfunc = Nothing
    End Sub

    Protected Sub ddlProviderDest_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlProviderDest.SelectedIndexChanged
        Dim lfunc As New LookUp.FillLookUp

        lfunc.FillDropDown(ddlProductDest, LookUp.FillLookUp.eLookUpType.prdt, , ddlProviderDest.SelectedValue)
        lfunc = Nothing
    End Sub

    Protected Sub btnCopyFunds_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCopyFunds.Click
        If Not isFundsInUse() Then
            Dim dm As New DAOMain.CommonDAO

            dm.AddParameter(Session("UserName"))
            dm.AddParameter(ddlProductSource.SelectedValue)
            dm.AddParameter(ddlProductDest.SelectedValue)

            dm.ExecCmdProc("sp_copy_fund")

            dm = Nothing
            ClearFields()
            DoSearch()
            pnlCopy.Visible = False
            divEntry.Visible = True

        Else
            Dim pf As New PolyFunc.ASPFunc
            pf.MessageAlert(Me, "Exiting funds in the destination are in use!")
            pf = Nothing
        End If
    End Sub

    Private Function isFundsInUse() As Boolean
        Dim blnReturn As Boolean = False
        'isExist
        Dim dao As New DAOAdmin.FUND_MSTR_DAO
        Dim vo As New DAOAdmin.FUND_MSTR_VO

        vo.PRDT_CD = ddlProductDest.SelectedValue
        Dim dt As DataTable = dao.Search(vo)

        If dt.Rows.Count > 0 Then
            Dim i As Integer

            For i = 0 To dt.Rows.Count - 1
                If isDeleteOK(dt.Rows(i)("fund_cd"), ddlProductDest.SelectedValue) = False Then
                    blnReturn = True
                    Exit For
                End If
            Next i
        End If

        dao = Nothing
        vo = Nothing

        Return blnReturn
    End Function

    Private Sub ClearFields()
        dgSource.DataSource = Nothing
        dgSource.DataBind()

        Dim obj As Object

        For Each obj In Me.pnlCopy.Controls
            If TypeOf obj Is DropDownList Then
                Dim ddl As DropDownList = obj

                Select Case ddl.ID
                    Case "ddlBrokerSource", "ddlBrokerDest"
                        ddl.SelectedIndex = 0
                    Case Else
                        ddl.Items.Clear()
                End Select
            End If
        Next
    End Sub
#End Region



End Class